package com.xc.shopping_font.Controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.shopping_font.common.Result;
import com.xc.shopping_font.entity.Cart;
import com.xc.shopping_font.entity.Goods;
import com.xc.shopping_font.entity.Orders;
import com.xc.shopping_font.entity.OrdersGoods;
import com.xc.shopping_font.service.CartService;
import com.xc.shopping_font.service.GoodsService;
import com.xc.shopping_font.service.OrdersGoodsService;
import com.xc.shopping_font.service.OrdersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Resource
    private OrdersService ordersService;


    private final String now = DateUtil.now();

    @Resource
    private OrdersGoodsService ordersGoodsService;

    @Resource
    private CartService cartService;

    @Resource
    private GoodsService goodsService;

    @RequestMapping("/getGoodsById/{id}")
    public Result getGoodsById(@PathVariable("id") Long id){
        QueryWrapper<OrdersGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",id);
        ArrayList<Goods> goods = new ArrayList<>();
        List<OrdersGoods> ordersGoodsList =  ordersGoodsService.list(queryWrapper);
        for (OrdersGoods ordersGoods : ordersGoodsList) {
            Long productId = ordersGoods.getProductId();
            Goods goods1 = goodsService.getById(productId);
            goods1.setQuantity(ordersGoods.getQuantity());
            goods.add(goods1);

        }
        return Result.success(goods);
    }











    //实现分页效果
    @GetMapping("/page")
    public IPage<Orders> findPage(@RequestParam Integer pageNum,
                                  @RequestParam Integer pageSize,
                                  @RequestParam(defaultValue = "") String uname,
                                  @RequestParam String cusname

    ){

        IPage<Orders> page= new Page<>(pageNum,pageSize);

        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("customer_name",cusname);

        if(!"".equals(uname)){
            queryWrapper.like("name",uname);
        }

        queryWrapper.orderByAsc("id");

        return ordersService.page(page,queryWrapper);
    }





    @PostMapping
    public Result save(@RequestBody Orders orders){
        if(orders.getId() == null){
            Date date =new Date();
            orders.setOrderTime(DateUtil.formatDateTime(date));
            orders.setNo(DateUtil.format(date,"yyyyMMdd") + System.currentTimeMillis());
            //        创建订单
            ordersService.save(orders);


            List<Cart> carts = orders.getCarts();
            for (Cart cart : carts) {
                OrdersGoods ordersGoods = new OrdersGoods();
                ordersGoods.setProductId(cart.getProductId());
                ordersGoods.setQuantity(cart.getQuantity());
                ordersGoods.setOrderId(orders.getId());
                ordersGoodsService.save(ordersGoods);

//删除购物车数据
                cartService.removeById(cart.getId());

            }

        }else{
            ordersService.updateById(orders);
        }

        return Result.success();
    }


    @DeleteMapping("/{id}")
    public Boolean deleteOrders(@PathVariable Integer id){
        return    ordersService.removeById(id);
    }






}
